
package cn.edu.ntu.yyl.controller.admin;


import cn.edu.ntu.yyl.vo.CommentVO;

import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.service.*;
import cn.edu.ntu.yyl.tool.OptionReaderTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminIndexServlet extends HttpServlet {
    @Autowired
    ArticleService articleService;
    @Autowired
    CommentService commentService;
    @Autowired
    CategoryService categoryService;
    OptionReaderTool optionReaderTool;// =  optionReaderTool.get();
    @Autowired
    CommentVOService commentVOService;
    @Autowired
    ArticleShowService articleShowService;

    //管理员登录页
    @RequestMapping({"/login", "/login.html"})
    public String adminLogin(){
        return "admin/login";
    }
    //管理员面板
    @RequestMapping({"/index", "/index.html","/dashboard","/dashboard.html"})
    public String DashboardIndex(HttpSession session, Model model , HttpServletRequest request){
        //获取参数
        String status = request.getParameter("date");
        if (status==null||status.equals("")){
            session.setAttribute("articles",articleService.SelectTodayArticle());
            session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectTodayComment()));
            status = "1";
        } else{
            Integer date = Integer.valueOf(status);
            switch (date) {
                case 2: {
                    session.setAttribute("articles", articleService.SelectYesterdayArticle());
                    session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectYesterdayComment()));
                    status = "2";
                    break;
                }
                case 7: {
                    session.setAttribute("articles", articleService.SelectSevenDayArticle());
                    session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectSevenDayComment()));
                    status = "7";
                    break;
                }
                case 14: {
                    session.setAttribute("articles", articleService.SelectFourteenDayArticle());
                    session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectFourteenDayComment()));
                    status = "14";
                    break;
                }
                case 30: {
                    session.setAttribute("articles", articleService.SelectMonthArticle());
                    session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectMonthComment()));
                    status = "30";
                    break;
                }
                case 1:
                default: {
                    session.setAttribute("articles", articleService.SelectTodayArticle());
                    session.setAttribute("ShowComments", commentVOService.Show(commentService.SelectTodayComment()));
                    status = "1";
                    break;
                }
            }
        }
        model.addAttribute("datestatus",status);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        model.addAttribute("articles",session.getAttribute("articles"));
        model.addAttribute("ShowComments",session.getAttribute("ShowComments"));
        return "admin/dashboard";
    }


    //文章管理
    /*
    @RequestMapping({"/article", "/article.html","/article/index","/article/index.html"})
    public String ArticleIndex(HttpSession session, Model model , HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //获取查询参数
        String status= request.getParameter("status");
        List<ArticleShow> articleShows;
        //查询数量
        Integer AllArticleCount = articleService.SelectAllArticle().size();
        Integer NormalArticleCount = articleService.SelectArticCountleByStatus("normal");
        Integer DraftArticleCount = articleService.SelectArticCountleByStatus("draft");
        Integer TrashArticleCount = articleService.SelectArticCountleByStatus("trash");
        if (status==null){
            //查询全部
            articleShows = articleShow.ShowAll();
        }
        else if (status.equals("normal")){
            //查询已发布
            articleShows = articleShow.ShowByStatus("normal");
        }else if (status.equals("draft")){
            //查询草稿箱
            articleShows = articleShow.ShowByStatus("draft");
        }else if (status.equals("trash")){
        //查询垃圾箱
            articleShows = articleShow.ShowByStatus("trash");
        }else{
            //查询全部
            articleShows = articleShow.ShowAll();
        }
        model.addAttribute("AllArticleCount",AllArticleCount);
        model.addAttribute("NormalArticleCount",NormalArticleCount);
        model.addAttribute("DraftArticleCount",DraftArticleCount);
        model.addAttribute("TrashArticleCount",TrashArticleCount);
        model.addAttribute("articleShows",articleShows);
        return "admin/article/index";
    }
    */
    //文章撰写
    @RequestMapping({"/article/write", "/article/write.html","/article/write/index","/article/write/index.html"})
    public String write(HttpSession session, Model model){
        //查询出所有分类和标签并且写入model
        List<ArticleCategory> categories = categoryService.SelectAllArticleCategory();
        List<ArticleCategory> tags = categoryService.SelectTag();
        model.addAttribute("tags",tags);
        model.addAttribute("categories",categories);
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        return "admin/article/write";
    }
    //分类管理
    @RequestMapping({"/article/category", "/article/category.html","/article/category/index","/article/category/index.html"})
    public String ArticleCategoryIndex(HttpSession session, Model model , HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //查询正在编辑的分类信息
        Integer categoryid = 0;
        if (request.getParameter("categoryid")!=null){
            String cid = request.getParameter("categoryid");
            categoryid = Integer.valueOf(cid);
        }
        if (categoryid!=null||categoryid!=0){
            ArticleCategory editCategory = categoryService.SelectCategoryById(categoryid);
            model.addAttribute("editCategory",editCategory);
        }
        //查询所有的分类
        List<ArticleCategory> articleCategoryList = categoryService.SelectAllArticleCategory();
        model.addAttribute("articleCategoryList",articleCategoryList);
        return "admin/article/category";
    }
    //标签管理
    @RequestMapping({"/article/tag", "/article/tag.html","/article/tag/index","/article/tag/index.html"})
    public String ArticleTagIndex(HttpSession session, Model model , HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //查询正在编辑的标签信息
        Integer tagid = 0;
        if (request.getParameter("tagid")!=null){
            String tid = request.getParameter("tagid");
            tagid = Integer.valueOf(tid);
        }
        if (tagid!=null||tagid!=0){
            ArticleCategory editTag = categoryService.SelectTagById(tagid);
            model.addAttribute("editTag",editTag);
        }
        //查询所有的标签
        List<ArticleCategory> TagList = categoryService.SelectTag();
        model.addAttribute("TagList",TagList);
        return "admin/article/tag";
    }
    //评论管理
    @RequestMapping({"/article/comment", "/article/comment.html","/article/comment/index","/article/comment/index.html"})
    public String ArticleCommentIndex(HttpSession session, Model model , HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //获取查询参数
        String status= request.getParameter("status");
        List<CommentVO> commentVOS;
        //查询数量
        Integer AllCommentCount = commentService.SelectAllComment().size();
        Integer NormalCommentCount = commentService.SelectCommentByStatus("normal").size();
        Integer UnauditedCommentCount = commentService.SelectCommentByStatus("unaudited").size();
        Integer TrashCommentCount = commentService.SelectCommentByStatus("trash").size();
        if (status==null){
            //查询全部
            commentVOS = commentVOService.Show(commentService.SelectAllComment());
        }
        else if (status.equals("normal")){
            //查询已发布
            commentVOS = commentVOService.Show(commentService.SelectCommentByStatus("normal"));
        }else if (status.equals("unaudited")){
            //查询未审核
            commentVOS = commentVOService.Show(commentService.SelectCommentByStatus("unaudited"));
        }else if (status.equals("trash")){
            //查询垃圾箱
            commentVOS = commentVOService.Show(commentService.SelectCommentByStatus("trash"));
        }else{
            //查询全部
            commentVOS = commentVOService.Show(commentService.SelectAllComment());
        }
        model.addAttribute("AllCommentCount",AllCommentCount);
        model.addAttribute("NormalCommentCount",NormalCommentCount);
        model.addAttribute("UnauditedCommentCount",UnauditedCommentCount);
        model.addAttribute("TrashCommentCount",TrashCommentCount);
        model.addAttribute("CommentShows", commentVOS);
        return "admin/article/comment";
    }
    //评论编辑
    @RequestMapping({"/article/commentEdit", "/article/commentEdit.html","/article/commentEdit/index","/article/commentEdit/index.html"})
    public String ArticlecommentEditIndex(HttpSession session, Model model , HttpServletRequest request){
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //查询正在编辑的标签信息
        Integer commentid = 0;
        if (request.getParameter("editcomment")!=null){
            String cid = request.getParameter("editcomment");
            commentid = Integer.valueOf(cid);
            model.addAttribute("editcomment", commentVOService.Show(commentService.SelectCommentById(commentid)));
        }
        return "admin/article/commentEdit";
    }
    //文章设置
    @RequestMapping({"/article/setting", "/article/setting.html","/article/setting/index","/article/setting/index.html"})
    public String ArticleSettingIndex(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //读取系统设置
        Map<String,Object> options = optionReaderTool.ReadOptions();
        //将系统设置写入session
        session.setAttribute("options",options);
        //将系统设置写入model
        model.addAttribute("options",options);

        return "admin/article/setting";
    }
    //附件设置
    @RequestMapping({"/attachment/setting", "/attachment/setting.html","/attachment/setting/index","/attachment/setting/index.html"})
    public String AttachmentSettingIndex(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //读取系统设置
        Map<String,Object> options = optionReaderTool.ReadOptions();
        //将系统设置写入session
        session.setAttribute("options",options);
        //将系统设置写入model
        model.addAttribute("options",options);

        return "admin/attachment/setting";
    }
    //系统设置
    @RequestMapping({"/system/setting", "/system/setting.html","/system/setting/index","/system/setting/index.html"})
    public String SystemSettingIndex(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //读取系统设置
        Map<String,Object> options = optionReaderTool.ReadOptions();
        //将系统设置写入session
        session.setAttribute("options",options);
        //将系统设置写入model
        model.addAttribute("options",options);

        return "admin/system/index";
    }
    //注册设置
    @RequestMapping({"/system/reg", "/system/reg.html","/system/reg/index","/system/reg/index.html"})
    public String RegSettingIndex(HttpSession session, Model model , HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=UTF-8");
        //取出用户信息并写入model
        model.addAttribute("userInfo",session.getAttribute("userInfo"));
        //读取系统设置
        Map<String,Object> options = optionReaderTool.ReadOptions();
        //将系统设置写入session
        session.setAttribute("options",options);
        //将系统设置写入model
        model.addAttribute("options",options);
        return "admin/system/reg";
    }
}
